<?php
/*
 * Created on 19.12.2005
 *
 * Sychronizer Token implementation 
 */
 
 class SynchronizerToken {
  
  /**
   * @access privat
   */
  var $token;
  /**
   * @access privat
   */
  var $newtoken;
  
  /**
   * @access private
   */
  function SynchronizerToken () {
    $this->token = $GLOBALS['env']->getSession("sync_token");
    $this->newtoken = $this->generateNewToken();
    $GLOBALS['env']->setSession("sync_token", $this->newtoken);
  }
  
  /**
   * @access private
   */
  function generateNewToken () {
    return uniqid(rand() * microtime(), true);    
  }
  
  /**
   * static
   */
  function getInstance () {
    static $obj;
    if (!is_object($obj)) {
        $obj = new SynchronizerToken();
    }
    return $obj;
  }
  
  function getHidden () {
    return '<input type="hidden" name="sync_token" value="'.$this->newtoken.'"/>';
  }
  
  function getToken () {
    return $this->newtoken;
  }
  
  function check () {
    if (!$this->token || ($GLOBALS['env']->param("sync_token") == $this->token))
        return true;
  }
    
 }
?>
